package com.malou.client.ventas.tienda;

import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.NumberFormat;
import com.malou.client.MalouConstants;
import com.malou.client.util.Utilities;

public class TiendaDialog extends Dialog {

	  protected HiddenField<String> id;
	  protected TextField<String> tienda;	  
	  protected NumberField alquiler;
	  protected TextField<String> ubicacion;
	  protected TextField<String> telefono;	  
	  protected Button cancelar;
	  protected Button guardar;
	  protected Status status;

	  private FormBinding formBindings;
	  private FormPanel panel ;
	 
    
	  private MalouConstants constants = GWT.create(MalouConstants.class);

	  public TiendaDialog() {
		super();
		setModal(true);
		setSize(400, 190);
		setResizable(false);
		setButtons("");
		
		panel = new FormPanel();		
		panel.setBorders(false);
	    panel.setBodyBorder(false);
	    panel.setPadding(5);
	    panel.setHeaderVisible(false);
	   
	    
	    formBindings = new FormBinding(panel, false);	    
	    addFields();
	    
	    setFocusWidget(tienda);
	    
	    Component comp[]={tienda,ubicacion,telefono,alquiler,guardar};
	    Utilities.Enter(comp);
	  }

	  @Override
	  protected void createButtons() {
	    super.createButtons();
	    
	    if (constants == null) {
	    	constants = GWT.create(MalouConstants.class);
		}
	    
	    status = new Status();
	    status.setBusy(constants.guardando());
	    status.hide();
	    status.setAutoWidth(true);
	    getButtonBar().add(status);
	    
	    getButtonBar().add(new FillToolItem());
	    
	    cancelar = new Button(constants.cancelar(),
				IconHelper.createPath("images/icons/cancel.png"));
		cancelar.setSize(70, 25);
	    cancelar.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {
	    	  TiendaDialog.this.hide();
	      }

	    });

	    guardar = new Button(constants.guardar(),
				IconHelper.createPath("images/icons/save_all.png"));
		guardar.setSize(70, 25);
	    guardar.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {	    	  
	    	   guardar();		  
	    		 
	    	  
	      }
	    });
	    
	    
	    
	    addButton(guardar);
	    addButton(cancelar);	    
	    
	  }
	  

	  protected boolean validate() {
		boolean tda = tienda.isValid();
		
	    return tda;
	  }
	  private void addFields() {

		id = new HiddenField<String>();
		id.setVisible(false);
		panel.add(id);
		
		tienda = new TextField<String>();
	    tienda.setMinLength(3);
	    tienda.setFieldLabel(constants.tiendaNombre());	    
	    tienda.setAllowBlank(false);	
	     
	    	    
	    alquiler = new NumberField();	       
	    alquiler.setFieldLabel(constants.tiendaCostoAlquiler());
	    alquiler.setFormat(NumberFormat.getFormat("0.00"));
	    alquiler.setMaxValue(100000);
	    alquiler.setAllowNegative(false);

	   
	    
	    ubicacion = new TextField<String>();
	    ubicacion.setMinLength(3);	    
	    ubicacion.setFieldLabel(constants.tiendaUbicacion());	    
	  
	    
	    telefono = new TextField<String>();
	    telefono.setMinLength(3);	
	    Utilities.setNumeric(telefono);
	    telefono.setFieldLabel(constants.tiendaTelefono());	  
	   
	
	    panel.add(setLayout(tienda, "95%", 90), new ColumnData(1));
		panel.add(setLayout(ubicacion, "95%", 90), new ColumnData(1));
		panel.add(setLayout(telefono, "50%", 90), new ColumnData(1));
		panel.add(setLayout(alquiler, "50%", 90), new ColumnData(1));
			    
	    add(panel);
	   
	  }
	  public void reset(){
		  setIconStyle("icon-new");
		  setHeading(constants.tiendaNuevaTienda());
		  		  
		  id.clear();
		  tienda.clear();		  
		  alquiler.clear();
		  ubicacion.clear();
		  telefono.clear();
		  
		  status.hide();
		  getButtonBar().enable();
	  }
	  public void setStore(ListStore<BeanModel> store){
		  
		  formBindings.addFieldBinding(new FieldBinding(id, "id"));
		  formBindings.addFieldBinding(new FieldBinding(tienda, "nombre"));
		  formBindings.addFieldBinding(new FieldBinding(alquiler, "costoalquiler"));
		  formBindings.addFieldBinding(new FieldBinding(ubicacion, "ubicacion"));
		  formBindings.addFieldBinding(new FieldBinding(telefono, "telefono"));
		  formBindings.setStore(store);
	  }
	  public void beforeEdit(){
		  setIconStyle("icon-update");
		  setHeading(constants.tiendaModificarTienda());		  
		  
		  status.hide();
		  getButtonBar().enable();
	  }
	  public void bind(BeanModel selectedModel){		  
		  formBindings.bind(selectedModel);
	  }
	  public void unbind(){
		  formBindings.unbind();
	  }
 private void guardar(){
	 if(validate()){
 		status.show();
		    getButtonBar().disable();
		    
		     Tienda tda = new Tienda();
			if(id.getValue() != null)
			tda.setId(Integer.valueOf(id.getValue()));
			tda.setNombre(tienda.getValue());    			
			if(Utilities.hasValue(alquiler))
			tda.setCostoalquiler(Double.valueOf(alquiler.getValue().toString()));
			tda.setUbicacion(ubicacion.getValue());
			tda.setTelefono(telefono.getValue());
			
			Tiendas.tiendaDialog = TiendaDialog.this;
			Tiendas.save(tda);	    			
 	  }	  
	 
 }	
 
 private LayoutContainer setLayout(Component comp, String porcentaje,Integer withlabel) {
		LayoutContainer layout = new LayoutContainer();
		layout.setStyleAttribute("marginTop", "5px");
		FormLayout fl = new FormLayout(LabelAlign.LEFT);
		fl.setLabelWidth(withlabel);
		layout.setLayout(fl);
		layout.add(comp, new FormData(porcentaje));

		return layout;
	}

}